﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Runtime.Serialization;
using System.Xml;

#endregion


namespace ESF.General.Entity
{
	[DataContract]
	[Serializable]
	public class PaggingInfo : IEquatable<PaggingInfo>
	{
		#region Constantes

		public static PaggingInfo Empty = new PaggingInfo ();

		public static PaggingInfo Max = new PaggingInfo() { PageSize = int.MaxValue };
		
		public const int DEFAULT_PAGESIZE = 10;

		#endregion

		
		[DataMember]
		public int PageIndex { set; get; }
		
		[DataMember]
		public int PageSize { set; get; }
		
		[DataMember]
		public int PageCount { get { return (int) Math.Ceiling (RowCount / (double) PageSize); } set { }  }
		
		[DataMember]
		public int RowCount { set; get; }

		
		public bool IsThereFirst { get { return PageIndex > 0; } }

		public bool IsTherePrevious { get { return PageIndex != 0; } }
		
		public bool IsThereNext { get { return PageIndex < PageCount - 1; } }

		public bool IsThereLast { get { return PageIndex + 1 < PageCount; } }
		

		public bool IsThereRows
		{
			get
			{
				return RowCount > 0;
			}
		}


		#region Constructor

		public PaggingInfo ()
		{
			PageIndex = 0;

			PageSize = DEFAULT_PAGESIZE;

			RowCount = 0;
		}

		#endregion

		
		#region Funciones

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}


		public void SetFirst ()
		{
			PageIndex = 0;
		}
		
		
		public void SetNext ()
		{
			if (PageIndex < PageCount)
			{
				PageIndex++;
			}
		}
		
		
		public void SetLast ()
		{
			PageIndex = PageCount - 1;
		}
		
		
		public void SetPrevious ()
		{
			if (PageIndex > 0)
			{
				PageIndex --;
			}
		}
		
		#endregion


		#region Methods Equals

		public bool Equals(PaggingInfo pOther)
		{
			if (pOther == null) { return false; }

			return (PageIndex == pOther.PageIndex && PageSize == pOther.PageSize && RowCount == pOther.RowCount);
		}


		public override bool Equals (object pOther)
		{
			return Equals (pOther as PaggingInfo);
		}

		#endregion


		public void ConvertToXML(XmlWriter pXmlWriter)
		{
			ConvertToXML (pXmlWriter, this);
		}


		public static void ConvertToXML(XmlWriter pXmlWriter, PaggingInfo pPagInfo)
		{
			if (pPagInfo == null)
			{
				return;
			}

			pXmlWriter.WriteStartElement("PaggingInfo");

				pXmlWriter.WriteAttributeString("IsThereFirst", pPagInfo.IsThereFirst.ToString());
				pXmlWriter.WriteAttributeString("IsThereLast", pPagInfo.IsThereLast.ToString());
				pXmlWriter.WriteAttributeString("IsThereNext", pPagInfo.IsThereNext.ToString());
				pXmlWriter.WriteAttributeString("IsTherePrevious", pPagInfo.IsTherePrevious.ToString());
				pXmlWriter.WriteAttributeString("PageCount", pPagInfo.PageCount.ToString());
				pXmlWriter.WriteAttributeString("PageIndex", pPagInfo.PageIndex.ToString());
				pXmlWriter.WriteAttributeString("PageSize", pPagInfo.PageSize.ToString());
				pXmlWriter.WriteAttributeString("RowCount", pPagInfo.RowCount.ToString());

			pXmlWriter.WriteEndElement();
		}
	}
}
